package com.yanyan.sannuomin.exception;


import com.yanyan.sannuomin.result.Result;
import com.yanyan.sannuomin.result.ResultCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 系统全局异常处理类
 * @author dml
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理其他异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public Result exceptionHandler(HttpServletRequest req, Exception e){

        return Result.error(ResultCode.FAIL);
    }

    /**
     * 处理自定义的业务异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = ResultException.class)
    @ResponseBody
    public  Result resultExceptionHandler(HttpServletRequest req, ResultException e){

        return Result.error(e.getErrorCode(),e.getErrorMsg());
    }

    /**
     * 处理空指针的异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =NullPointerException.class)
    @ResponseBody
    public Result exceptionHandler(HttpServletRequest req, NullPointerException e){
        return Result.error("111","空指针异常");
    }
}
